﻿using Leeder.Logger;
using PartsRetrospect.Business;
using PartsRetrospect.Comm;
using PartsRetrospect.Handle;
using PartsRetrospect.Model;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PartsRetrospect.Task
{
    /// <summary>
    /// SFTP数据上传作业
    /// </summary>
    public class SFTPUploadTask : IJob
    {
        /// <summary>
        /// 日志
        /// </summary>
        public LogOperate LogObj => Log.GetLogger(this.GetType());
        FtpUploadHandler ftpUploadHandler = new FtpUploadHandler();
        UploadSetBLL uploadSetBLL = new UploadSetBLL();
        List<AirbagModel> airbagList = new List<AirbagModel>();
        AirbagBLL airbagBLL = new AirbagBLL();

        public void Execute(IJobExecutionContext context)
        {
            string errMesg = "";
            airbagList = airbagBLL.GetAirbagsNotUpload();
            if (airbagList.Count > 0)
            {
                UploadSetModel model = uploadSetBLL.GetModel();
                ftpUploadHandler.SFtpIp = model.IP;
                ftpUploadHandler.SFtpPort = model.Port;
                ftpUploadHandler.SFtpUser = model.UserName;
                ftpUploadHandler.SFtpPwd = model.Password;
                ftpUploadHandler.UploadFilePath = model.UploadPath;
                ftpUploadHandler.workshop = GolbalVariable.WorkShopSet.WorkShopNo.ToString();
                ftpUploadHandler.computerNo = GolbalVariable.WorkShopSet.ComputerNo;
                if (ftpUploadHandler.UploadData(airbagList, ref errMesg))
                {
                    LogObj.Info("气囊数据上传成功，上传数量:" + airbagList.Count);
                    if(airbagBLL.UpdateUploadState(airbagList))
                        LogObj.Info("气囊数据上传状态更新成功！");
                    else
                        LogObj.Error("气囊数据上传状态更新失败！");
                }
                else
                {
                    LogObj.Error("气囊数据上传失败，失败原因:" + errMesg);
                }
            }
        }
    }
}
